package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.VipServiceConfigService;
import com.atguigu.tingshu.user.strategy.ProcessPaidRecord;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author codingyz
 * @Description
 * @date 2025/3/30 15:03
 * @Version 1.0
 */
@Service("1003")
@Slf4j
public class VipItemType implements ProcessPaidRecord {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigService vipServiceConfigService;

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public void processDiffItemTypePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        Long userId = userPaidRecordVo.getUserId();
        String orderNo = userPaidRecordVo.getOrderNo();
        String itemType = userPaidRecordVo.getItemType();
        List<Long> itemIdList = userPaidRecordVo.getItemIdList();

        // 1. 幂等性校验
        Long count = userVipServiceMapper.selectCount(new LambdaQueryWrapper<UserVipService>().eq(UserVipService::getOrderNo, orderNo).eq(UserVipService::getUserId, userId));
        if (count != 0) {
            // 已经存过了，直接返回
            log.error("该声音订单流水已经记录");
            return;
        }
        // 2. 保存订单流水
        UserVipService userVipService = new UserVipService();
        userVipService.setOrderNo(orderNo);
        userVipService.setUserId(userId);
        userVipService.setVipConfigId(itemIdList.get(0).intValue());
        userVipService.setStartTime(new Date()); // 套餐开始时间 直接用当前时间
        // 查询该套餐的时长
        VipServiceConfig vipServiceConfig = vipServiceConfigService.getById(itemIdList.get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();// 套餐时长/月
        // 查询该用户是否vip还没过期
        UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
        Integer isVip = userInfoVo.getIsVip();
        Calendar calendar = Calendar.getInstance();// 日历对象 方便操作时间
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, serviceMonth);
        Date expireTime = calendar.getTime();
        if (isVip == 1 && userInfoVo.getVipExpireTime().after(new Date())) {
            // vip没过期
            calendar.setTime(userInfoVo.getVipExpireTime());
            calendar.add(Calendar.MONTH, serviceMonth);
            expireTime = calendar.getTime();
        }
        userVipService.setExpireTime(expireTime); // 需要判断 该用户是否vip还没过期 如果没过期 则是续费
        userVipServiceMapper.insert(userVipService);
        log.info("保存vip订单流水{}", "成功");
        // 3. 更新用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userId);
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
        boolean b = userInfoService.updateById(userInfo);
        log.info("更新用户vip信息{}", b ? "成功" : "失败");
    }
}
